package org.unedvirtualslate.gui;

import java.awt.FlowLayout;

import javax.swing.GroupLayout;
import javax.swing.JFrame;

import org.unedvirtualslate.logic.CanvasPanel;
import org.unedvirtualslate.logic.MainWindowLogic;
import org.unedvirtualslate.logic.ToolButtonPanelLogic;

public class MainWindow extends JFrame {
	private static final long serialVersionUID = 1L;	
	private CanvasPanel canvasPanel;
	private ToolButtonPanel buttonPanel;
	private ToolButtonPanelLogic logicCanvas;

	/**
	 * This is the default constructor
	 */
	public MainWindow() {
		super();
		initialize();
		this.canvasPanel.setDrawMode(CanvasPanel.FREE_HAND);
	}
	
	/**
	 * Constructor parametrizado.
	 * @param logic
	 */
	public MainWindow(MainWindowLogic logic){
		this();
		this.canvasPanel.setDrawMode(CanvasPanel.FREE_HAND);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setSize(800, 600);
		this.getContentPane().setSize(800, 600);
		this.setTitle("Uned Virtual Slate");
		this.getCanvasPanel();
		this.getButtonPanel();

        FlowLayout jPanelButtonPanel = new FlowLayout(FlowLayout.LEFT);
        this.buttonPanel.setLayout(jPanelButtonPanel);
//        jPanelButtonPanel.setHorizontalGroup(
//            jPanelButtonPanel.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 100, Short.MAX_VALUE)
//        );
//        jPanelButtonPanel.setVerticalGroup(
//            jPanelButtonPanel.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 360, Short.MAX_VALUE)
//        );

        GroupLayout jPanelCanvasPanel = new GroupLayout(this.canvasPanel);
        this.canvasPanel.setLayout(jPanelCanvasPanel);
        jPanelCanvasPanel.setHorizontalGroup(
            jPanelCanvasPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 333, Short.MAX_VALUE)
        );
        jPanelCanvasPanel.setVerticalGroup(
            jPanelCanvasPanel.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 315, Short.MAX_VALUE)
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(this.buttonPanel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE) // El 105 es el tamaño del panel de botones.
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(this.canvasPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(this.buttonPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(this.canvasPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
	}
	
	private CanvasPanel getCanvasPanel(){
		if(this.canvasPanel == null){
			this.canvasPanel = new CanvasPanel();
		}
		//this.canvasPanel.setVisible(true);
		return this.canvasPanel;
	}	
	
	private ToolButtonPanel getButtonPanel(){
		if(this.buttonPanel == null){
			if(this.logicCanvas == null){
				this.logicCanvas = new ToolButtonPanelLogic(this.getCanvasPanel());
			}
			this.buttonPanel = new ToolButtonPanel(this.logicCanvas);
		}
		//this.buttonPanel.setVisible(true);
		return this.buttonPanel;
	}

}  //  @jve:decl-index=0:visual-constraint="0,0"
